package com.campus.business.controller;

import com.campus.common.dto.ResponseDto;
import com.campus.common.dto.order.OrderDto;
import com.campus.common.enums.OrderStatusEnum;
import com.campus.common.mapper.OrderMapper;
import com.campus.common.service.OrderService;
import com.campus.common.vo.OrderVo;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.PortResolverImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 * @program: campus
 * @description:
 * @author: ENDING
 * @create: 2024-04-25 23:22
 **/

@Slf4j
@CrossOrigin
@Data
@RestController
@Transactional
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("测试")
    @GetMapping("/test")
    public ResponseDto<String> test() {

        return new ResponseDto<>(true, "200", "success", "success");
    }

    @ApiOperation("创建订单")
    @PostMapping("/create")
    public ResponseDto createOrder(@RequestBody OrderDto orderDto) {

        OrderVo orderVo = orderService.createOrder(orderDto);

        return new ResponseDto<OrderVo>(true, "200", "订单创建成功，请30分钟内支付", orderVo);
    }

    @ApiOperation("订单支付")
    @PostMapping("/pay/{orderId}")
    public ResponseDto orderPay(@PathVariable Long orderId) {

        OrderVo orderVo = orderService.payOrder(orderId);
         if(orderVo != null) {
             return new ResponseDto<OrderVo>(true, "200", "支付成功", orderVo);
         }

        return new ResponseDto<String>(false, OrderStatusEnum.PAY_ERROR.getCode(),
                OrderStatusEnum.PAY_ERROR.getDesc(), OrderStatusEnum.PAY_ERROR.getDesc());
    }

    @ApiOperation("删除订单")
    @PostMapping("/del/{orderId}")
    public ResponseDto orderDelete(@PathVariable Long orderId) {

        if(orderService.deleteOrder(orderId)) {
            return new ResponseDto(true, "200", "删除成功", "");
        }

        return new ResponseDto(false, "1102", "删除失败", "");
    }

    @ApiOperation("获取所有订单")
    @GetMapping("/all/{userId}")
    public ResponseDto getAllOrder(@PathVariable Long userId) {

        return new ResponseDto(true, "200", "success", orderService.getAllOrder(userId));
    }
}
